package com.qdxwx.web.controller.ajax;

import com.qdxwx.common.cache.CacheUtils;
import com.qdxwx.common.constants.CacheConstants;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.advert.AdvertService;
import com.qdxwx.models.advert.Advert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
/*
* api 通用获取广告图
* */
@Controller
@RequestMapping("/ajax/advert")
public class AjaxAdvertController {

    @Autowired
    private AdvertService advertService;

    @RequestMapping("/list.json")
    @ResponseBody
    public AjaxResult bannerList(HttpServletRequest request) {
        int typeId = TKConvert.toInteger(request.getParameter("typeId"), 1);//默认轮播
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        String cacheKey = CacheConstants.ADVERT_LIST + ":advert:" + channelId;
        List<Advert> advertList = (List<Advert>) CacheUtils.get(cacheKey);
        if (advertList == null || advertList.size() < 1) {
            Advert advert = new Advert();
            advert.setTypeId(typeId);
            advert.setChannelId(channelId);
            advertList = advertService.getList(advert, null);
            CacheUtils.set(cacheKey, advertList, CacheConstants.BANNER_IMAGES_TIME);
        }
        return AjaxResult.success(advertList);
    }
}
